package com.woniuxy.core.service.impl;

import com.woniuxy.core.mapper.RunMapper;
import com.woniuxy.core.service.RunExpirationService;
import com.woniuxy.entity.AllRun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RunExpirationServiceImpl implements RunExpirationService {
    @Autowired
    private RunMapper runMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
//定时任务用于定期清除已经过期的跑腿任务
    @Transactional
    public void handleExpiredRuns() {
        // 1. 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 2. 查询已过期的未处理任务
        List<AllRun> expiredRuns = runMapper.selectExpiredRuns(now);

        // 3. 批量更新状态
        expiredRuns.forEach(run -> {
            // 更新数据库状态（例如设为过期状态）
            runMapper.updateRunState(run.getId());
        });
    }
}
